package ustc.xiaoyang.karl.doublepointer;

import java.util.Arrays;

public class SquareOfAnOrderedArray977 {
    public static void main(String[] args) {

        int [] nums = {-4,-1,0,3,10};
        System.out.println(Arrays.toString(new SquareOfAnOrderedArray977().sortedSquares(nums)));
    }
    public int[] sortedSquares(int[] nums){
        int left = 0, right = nums.length - 1;
        int[] res = new int[nums.length];
        int index = nums.length - 1;
        while(left <= right){
            int leftSquare = nums[left] * nums[left];
            int rightSquare = nums[right] * nums[right];
            if(leftSquare<rightSquare){
                res[index--] = rightSquare;
                right--;
            }else {
                res[index--] = leftSquare;
                left++;
            }
        }
        return res;
    }
}
